package com.lzk.seckill.web; /**
 * author: 刘中魁
 */

import com.lzk.seckill.redis.SecKillRedis;
import com.lzk.seckill.redis.SecKillRedisByLua;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Random;

public class SecKillServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 请求时，模拟生成一个Uid
        String userID =  new Random().nextInt(10000) + "";
        //2. 获取用户要购买的票的编号
        String ticketNo = request.getParameter("ticketNo");
        //3.  调用秒杀方法
//        boolean isOk = SecKillRedis.doSecKill(userID, ticketNo);
        // 4. 调用lua脚本完成秒杀方法
        boolean isOk = SecKillRedisByLua.doSecKill(userID, ticketNo);

        // 将结果返回给前端
        response.getWriter().print(isOk);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request,response);
    }
}
